/**
 *  Copyright (C) 2008-2009  The Aceituna Project Team
 *  Portions of this code are Copyright(C) 2002-2007  The FreeCol Team (www.freecol.org).
 *
 *  This file is part of Aceituna Project.
 *
 *  Aceituna Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Aceituna Project is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Aceituna Project.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aceitunaproject.mapdisplay.model.map.tile;

import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import java.util.List;

import com.aceitunaproject.mapdisplay.model.ModelObject;
import com.aceitunaproject.mapdisplay.model.Thing;
import com.aceitunaproject.mapdisplay.model.map.Map;
import com.aceitunaproject.mapdisplay.model.map.Position;

/**
 * Represents a single tile on the <code>Map</code>.
 * 
 * @see Map
 */
public final class Tile extends ModelObject {

	private int x, y;

	/**
	 * Stores all Things (if any).
	 */
	private List<Thing> things = Collections.emptyList();
	
	private int left;
	/**
	 * @return the right
	 */
	public int getRight() {
		return right;
	}

	/**
	 * @param right the right to set
	 */
	public void setRight(int right) {
		this.right = right;
	}

	/**
	 * @return the top
	 */
	public int getTop() {
		return top;
	}

	/**
	 * @param top the top to set
	 */
	public void setTop(int top) {
		this.top = top;
	}

	/**
	 * @return the bottom
	 */
	public int getBottom() {
		return bottom;
	}

	/**
	 * @param bottom the bottom to set
	 */
	public void setBottom(int bottom) {
		this.bottom = bottom;
	}

	private int right;
	private int top;
	private int bottom;

	private String basicTileType;




	/**
	 * A constructor to use.
	 * 
	 * @param type
	 *            The type.
	 * @param locX
	 *            The x-position of this tile on the map.
	 * @param locY
	 *            The y-position of this tile on the map.
	 */
	public Tile(String basicTileType, int locX, int locY) {

		x = locX;
		y = locY;
		this.basicTileType = basicTileType;


	}

	/**
	 * Gets the first <code>Thing</code> on this tile.
	 * 
	 * @return The first <code>Thing</code> on this tile.
	 */
	public Thing getFirstThing() {
		if (things.isEmpty()) {
			return null;
		} else {
			return things.get(0);
		}
	}

	/**
	 * Gets the last <code>Thing</code> on this tile.
	 * 
	 * @return The last <code>Thing</code> on this tile.
	 */
	public Thing getLastThing() {
		if (things.isEmpty()) {
			return null;
		} else {
			return things.get(things.size() - 1);
		}
	}

	/**
	 * Returns the x-coordinate of this Tile.
	 * 
	 * @return The x-coordinate of this Tile.
	 */
	public int getX() {
		return x;
	}

	/**
	 * Returns the y-coordinate of this Tile.
	 * 
	 * @return The y-coordinate of this Tile.
	 */
	public int getY() {
		return y;
	}

	/**
	 * Gets the <code>Position</code> of this <code>Tile</code>.
	 * 
	 * @return The <code>Position</code> of this <code>Tile</code>.
	 */
	public Position getPosition() {
		return new Position(x, y);
	}

	/**
	 * Gets the <code>Tile</code> where this <code>Location</code> is located or
	 * null if no <code>Tile</code> applies.
	 * 
	 * @return This <code>Tile</code>.
	 */
	public Tile getTile() {
		return this;
	}

	/**
	 * Removes the Thing from the tile.
	 * 
	 * @param thing
	 *            The Thing to be removed
	 */
	public void removeThing(Thing thing) {
		things.remove(thing);
	}

	/**
	 * Adds the thing to the tile.
	 * 
	 * @param thing
	 *            The thing to be added
	 */
	public void addThing(Thing thing) {
		if (things.equals(Collections.emptyList())) {
			things = new ArrayList<Thing>();
		}
		things.add(thing);
	}

	/**
	 * Returns the amount of things at this <code>Location</code>.
	 * 
	 * @return The amount of things at this <code>Location</code>.
	 */
	public int getThingCount() {
		return things.size();
	}

	/**
	 * Gets a <code>List/code> of every <code>Thing</code> directly located on
	 * this <code>Tile</code>. This does not include <code>Thing</code>s located
	 * in a <code>Settlement</code> or on another <code>Thing</code> on this
	 * <code>Tile</code>.
	 * 
	 * @return The <code>List</code>.
	 */
	public List<Thing> getThingList() {
		return things;
	}

	/**
	 * Gets an <code>Iterator</code> of every <code>Thing</code> directly
	 * located on this <code>Tile</code>. This does not include
	 * <code>Thing</code>s located in a <code>Settlement</code> or on another
	 * <code>Thing</code> on this <code>Tile</code>.
	 * 
	 * @return The <code>Iterator</code>.
	 */
	public Iterator<Thing> getThingIterator() {
		return things.iterator();
	}

	

	/**
	 * @param basicTileType the basicTileType to set
	 */
	public void setBasicTileType(String basicTileType) {
		this.basicTileType = basicTileType;
	}

	/**
	 * @return the basicTileType
	 */
	public String getBasicTileType() {
		return basicTileType;
	}

	/**
	 * @param left the left to set
	 */
	public void setLeft(int left) {
		this.left = left;
	}

	/**
	 * @return the left
	 */
	public int getLeft() {
		return left;
	}
	
	public Polygon area;
	

}
